﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Presenters
{
    public class AnnotationSetContextMenuProvider
    {
        public AnnotationSetContextMenuProvider(AnnotationMenuProviderRegistry menuProviders, IMenuFactory menuFactory)
        {
            this.MenuProviderRegistry = menuProviders;
            this.MenuFactory = menuFactory;
        }

        public IMenu CreateContextMenu(AnnotationInfoSet annotationSet)
        {
            IMenu contextMenu = this.MenuFactory.CreateContextMenu();

            foreach (AnnotationInfo a in annotationSet.Annotations)
            {
                if (a.Status == AnnotationRequestStatus.Finished)
                {
                    //get all providers for current annotation
                    var providers = this.MenuProviderRegistry.GetMenuProvidersForAnnotation(a.Value);
                    foreach (var provider in providers)
                    {
                        //provider can return null if no menu can be created for the annotation
                        IMenuItem menuItem = provider.CreateMenu(a.Value);

                        if (menuItem != null)
                            contextMenu.AddItem(menuItem);
                    }
                }
                else
                {
                    //if the annotation is not complete, create a disabled menu with the descriptor text
                    IMenuItem menuItem = this.MenuFactory.CreateAnnotationContextMenuItem(a.Status);
                    menuItem.DisplayText = a.Descriptor.Name;
                    menuItem.IsEnabled = false;

                    contextMenu.AddItem(menuItem);
                }
            }

            return contextMenu;
        }

        private AnnotationMenuProviderRegistry MenuProviderRegistry { get; set; }
        private IMenuFactory MenuFactory { get; set; }
    }
}
